// 导入数据库操作模块
const { reject } = require('async')
const { resolve } = require('path/posix')
const db = require('../../db/index')

// 获取分类与标签
exports.getTypeLabel = (req, res) => {
    const sql1 = `select * from type where t_state=1`
    const sql2 = `select * from label where l_state=1`
    db.query(`${sql1};${sql2}`, (err, result) => {
        if(err) return res.operationSuccess('failure', 'ERR081', '获取分类标签信息失败！', err)
        let allArr = []
        result[0].forEach(type => {
            let obj = {tid: type.tid, t_name: type.t_name, label:[]}
            result[1].forEach(label => {
                if(type.tid == label.tid) obj.label.push(label)
            })
            allArr.push(obj)
        })
        return res.operationSuccess('success', '', '获取分类标签信息成功！', allArr)
    })
}

// 根据诗歌id对获取到的分类与标签进行分组
exports.groupTypeLabelByPid = (req, res) => {
    // const sql1 = `select l.lid, l.l_name, t.tid, t.t_name from label l, type t where l.tid = t.tid and t.t_state=1 and l.l_state=1`
    const sql1 = `select p.pid, p.p_title, l.l_name, l.lid, t.tid, t.t_name, pl_id from poetry2 p, label l, type t, poetry_label pl where p.pid=pl.pid and pl.lid=l.lid and l.tid=t.tid and t.t_state=1 and l.l_state=1 and pl.pl_state=1`
    db.query(sql1, (err1, result1) => {
        if(err1) return res.operationSuccess('failure', 'ERR081', '获取分类标签信息失败！', err1)
        let obj = {}
        result1.forEach(item => {
            if(obj[item.pid]) {
                // obj[item.pid].label.push({lid: item.lid, l_name: item.l_name})
                obj[item.pid].label.push({tid: item.tid, t_name: item.t_name, lid: item.lid, l_name: item.l_name, pl_id: item.pl_id})
            } else {
                // obj[item.pid] = {p_title: item.p_title, tid: item.tid, t_name: item.t_name, label:[{lid: item.lid, l_name: item.l_name}]}
                obj[item.pid] = {p_title: item.p_title, label:[{tid: item.tid, t_name: item.t_name, lid: item.lid, l_name: item.l_name, pl_id: item.pl_id}]}
            }
        })
        return res.operationSuccess('success', '', '获取分类标签信息成功！', obj)
    })
}

// 获取标签信息
exports.getAllLabel = (req, res) => {
    const sql = `select * from label`
    db.query(sql, (err, result) => {
        if(err) return res.operationSuccess('failure', 'ERR084', '获取标签信息失败！', err)
        return res.operationSuccess('success', '', '获取标签信息成功！', result)
    })
}

// 根据tid添加标签
// 
// 立春、雨水、惊蛰、
// 春分、清明、谷雨、
// 立夏、小满、芒种、
// 夏至、小暑、大暑、
// 立秋、处暑、白露、
// 秋分、寒露、霜降、
// 立冬、小雪、大雪、
// 冬至、小寒、大寒
exports.getAddNewLabelByTid = (req, res) => {
    const { tid, label } = req.body
    let labelArr = label.split('、')
    let arr = []
    labelArr.forEach(item => {
        let a = []
        a.push(`${tid}`)
        a.push(`${item}`)
        a.push(1)
        arr.push(a)
    })
    // [[], []]
    const sql = `insert into label(tid, l_name, l_state) values ?`
    db.query(sql, [arr], (err, result) => {
        console.log('err', err)
        if(err) return res.operationSuccess('failure', 'ERR087', '添加新标签信息失败！', err)
        return res.operationSuccess('success', '', '添加新标签信息成功！', result)
    })
}

// 根据lid删除标签
exports.getDeleteLabelByLid = (req, res) => {
    const { lid } = req.body
    const sql = `update label set l_state=0 where lid=${lid}`
    db.query(sql,  (err, result) => {
        console.log('err', err)
        if(err) return res.operationSuccess('failure', 'ERR088', '删除标签信息失败！', err)
        return res.operationSuccess('success', '', '删除标签信息成功！', result)
    })
}

// 根据tid删除分类
exports.deleteTypeByTid = (req, res) => {
    const { tid } = req.body
    const sql = `update type set t_state=0 where tid=${tid}`
    const sql1 = `update label set l_state=0 where tid=${tid}`
    db.query(`${sql};${sql1}`,  (err, result) => {
        console.log('err', err)
        if(err) return res.operationSuccess('failure', 'ERR089', '删除分类信息失败！', err)
        return res.operationSuccess('success', '', '删除分类信息成功！', result)
    })
}


// 根据pid添加诗歌标签
exports.addPoetryLabelByPid = (req, res) => {
    const { pid, addLidString, deleteLidString } = req.body
    console.log('addPoetryLabelByPid.body', req.body)
    let sql = ''
    let deleteSql = `delete from poetry_label where pid=${pid} and lid in (${deleteLidString})`
    let insertSql = `insert into poetry_label(pid, lid, pl_state) values ?`
    if(deleteLidString != '') sql += `${deleteSql};`
    let arr = []
    if(addLidString != '') {
        sql += `${insertSql}`
        addLidString.toString().split(',').forEach((item) => {
            let arr1 = []
            arr1.push(`${pid}`)
            arr1.push(`${item}`)
            arr1.push(1)
            arr.push(arr1)
        })
    }
    db.query(sql, [arr], (err2, selectRes) => {
        console.log(err2)
        if(err2) return res.operationSuccess('failure', 'ERR085', '根据pid添加诗歌标签失败！', err2)
        return res.operationSuccess('success', '', '根据pid添加诗歌标签信息成功！', [])
    })
}

// 根据pid更新诗歌标签
exports.updatePoetryLabelByPid = (req, res) => {
    console.log('req', req.body)
    // const { pid, lidString } = req.body
    // console.log('pid', pid)
    // // let lidArr = lidString.split(',')
    // const selectSql = `select * from poetry_label where pid=${pid} and lid in (${lidString})`
    // db.query(selectSql, (err1, result1) => {
    //     if(err1) return res.operationSuccess('failure', 'ERR081', '获取分类标签信息失败！', err1)
    //     let lidArr1 = lidString.toString().split(',')
    //     let lidArr2 = lidString.toString().split(',')
    //     let arr1 = []
    //     let arr2 = []
    //     if(result1.length != 0) {
    //         result1.forEach(item => {
    //             arr1.push(lidArr1.find(target => { return parseInt(target) != item.lid }))
    //             lidArr2 = lidArr2.filter(target => { return parseInt(target) != item.lid })
    //         })
    //     }
    //     // let updataSql = ''
    //     console.log('arr1', arr1)
    //     console.log('lidArr2', lidArr2)
    //     // if(result1.length != 0) {
    //     //     updataSql = `update poetry_label set pl_state=1 where pid=${pid} and lid in (${lidString})`
    //     // } else {
    //     //     updataSql = `insert into poetry_label(pid, lid, pl_state) values(${pid}, ${lid}, 1)`
    //     // }
    //     // db.query(updataSql, (err2, result2) => {
    //     //     if(err2) return res.operationSuccess('failure', 'ERR083', '添加分类标签信息失败！', err2)
    //     //     return res.operationSuccess('success', '', '添加分类标签信息成功！', [])
    //     // })
    // })
}

// 根据pid删除诗歌标签
exports.deletePoetryLabelByPid = (req, res) => {
    const { pid, lid } = req.body
    sql = `delete from poetry_label where pid=${pid} and lid=${lid}`
    console.log('sql', sql)
    db.query(sql, (err1, result1) => {
        if(err1) return res.operationSuccess('failure', 'ERR082', '删除诗歌分类标签信息失败！', err1)
        return res.operationSuccess('success', '', '删除诗歌分类标签信息成功！', [])
    })
}

// 根据tid更改分类名
exports.updateTypeByTid = (req, res) => {
    const { t_name, tid } = req.body
    console.log('t_name', t_name)
    console.log('tid', tid)
    sql = `update type set t_name='${t_name}' where tid=${tid}`
    console.log('sql', sql)
    db.query(sql, (err1, result1) => {
        if(err1) return res.operationSuccess('failure', 'ERR080', '更改分类信息失败！', err1)
        return res.operationSuccess('success', '', '更改分类信息成功！', [])
    })
}

// 添加分类
exports.addType = (req, res) => {
    const { typeName, labelName } = req.body
    console.log(typeName, labelName)
    const sql1 = `insert into type(t_name, t_state) values('${typeName}', 1)`
    console.log('sql1', sql1)
    db.query(sql1, (err1, result1) => {
        if(err1) return res.operationSuccess('failure', 'ERR083', '添加分类或标签信息失败！', err1)
        console.log('result1', result1.insertId)
        let labelArr = labelName.split('、')
        let arr = []
        labelArr.forEach(item => {
            let a = []
            a.push(`${result1.insertId}`)
            a.push(`${item}`)
            a.push(1)
            arr.push(a)
        })
        const sql2 = `insert into label(tid, l_name, l_state) values?`
        db.query(sql2, [arr], (err2, result2) => {
            if(err2) return res.operationSuccess('failure', 'ERR083', '添加分类或标签信息失败！', err2)
            return res.operationSuccess('success', '', '添加标签信息成功！', [])
        })
    })
}


